package com.dcy.observer;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author：dcy
 * @Description:
 * @Date: 2021/1/28 11:10
 */
public class WeatherData implements Subject {

    //温度，气压，湿度
    private float temperature;
    private float pressure;
    private float humidity;
    //观察者集合
    private List<Observer> observers;

    public WeatherData() {
        this.observers = new ArrayList<>();
    }

    //当数据有更新时，就调用  setData
    public void setData(float temperature, float pressure, float humidity) {
        this.temperature = temperature;
        this.pressure = pressure;
        this.humidity = humidity;
        //调用 dataChange，将最新的信息推送给接入方currentConditions
        dataChange();
    }

    /**
     * 通知所有
     */
    public void dataChange() {
        notifyObserver();
    }

    @Override
    public void registerObserver(Observer observer) {
        observers.add(observer);
    }

    @Override
    public void removeObserver(Observer observer) {
        observers.remove(observer);
    }

    @Override
    public void notifyObserver() {
        for (Observer observer : this.observers) {
            observer.update(this.temperature, this.pressure, this.humidity);
        }
    }
}
